//笔试强训: 两个链表的第一个公共结点

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
		if(pHead1 == nullptr || pHead2 == nullptr)
			return nullptr;
        ListNode* cur1 = pHead1, *cur2 = pHead2;
		int count1 = 0, count2 = 0;
		while(cur1->next != nullptr)
		{
			cur1 = cur1->next;
			count1++;
		}
		
		while(cur2->next != nullptr)
		{
			cur2 = cur2->next;
			count2++;
		}

		cur1 = pHead1; cur2 = pHead2;
		while(cur1 != nullptr && count1 > count2)
		{
			cur1 = cur1->next;
			count1--;
		}
		while(cur2 != nullptr && count1 < count2)
		{
			cur2 = cur2->next;
			count2--;
		}

		while(cur1 != cur2 && cur1 != nullptr && cur2 != nullptr)
		{
			cur1 = cur1->next;
			cur2 = cur2->next;
		}

		return cur1;
    }
};
